using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class AskForNavalInterceptionDialog : Form
    {
        private List<FleetPoint> friendlyFleetPoints;
        private List<FleetPoint> enemyFleetPoints;
        List<PlayingPiece> piecesBeingTransported;

        public AskForNavalInterceptionDialog(SeaMoveOperationalMove seaMove, IGameStateInfoProvider gameInfoProvider)
        {
            InitializeComponent();
            this.askNavalInterceptionDialogTitleLabel.Text = "Intercept units En route to " + Enum.GetName(typeof(MapInfo.RegionID), seaMove.DisEmbarkationRegion) + " through " + Enum.GetName(typeof(MapInfo.RegionID), seaMove.SeaRegionTransportedThrough);
            if(seaMove.Player == GameController.playerType.AxisPlayer)
            {
                if(seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.BalticSea)
                {
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMove.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMove.SeaRegionTransportedThrough);
                }
                else if(seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.AtlanticOcean)
                {
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMove.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMove.SeaRegionTransportedThrough);
                    enemyFleetPoints.AddRange(gameInfoProvider.GetFleetPointList(MapInfo.CountryID.France, seaMove.SeaRegionTransportedThrough));
                }
                else if(seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.MediteraneanSea)
                {
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Italy, seaMove.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMove.SeaRegionTransportedThrough);
                    enemyFleetPoints.AddRange(gameInfoProvider.GetFleetPointList(MapInfo.CountryID.France, seaMove.SeaRegionTransportedThrough));
                }
            }
            else
            {
                if (seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.BalticSea)
                {
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMove.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMove.SeaRegionTransportedThrough);
                }
                else if (seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.AtlanticOcean)
                {
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMove.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMove.SeaRegionTransportedThrough);
                    friendlyFleetPoints.AddRange(gameInfoProvider.GetFleetPointList(MapInfo.CountryID.France, seaMove.SeaRegionTransportedThrough));
                }
                else if (seaMove.SeaRegionTransportedThrough == MapInfo.RegionID.MediteraneanSea)
                {
                    enemyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.Italy, seaMove.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMove.SeaRegionTransportedThrough);
                    friendlyFleetPoints.AddRange(gameInfoProvider.GetFleetPointList(MapInfo.CountryID.France, seaMove.SeaRegionTransportedThrough));
                }
                else
                {
                    enemyFleetPoints = new List<FleetPoint>();
                    friendlyFleetPoints = gameInfoProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMove.SeaRegionTransportedThrough);
                }
            }

            piecesBeingTransported = seaMove.GetListOfPiecesBeingTransported();
            this.listViewEnemyFleets.View = View.Tile;
            this.listViewEnemyFleets.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.listViewFriendlyFleets.View = View.Tile;
            this.listViewFriendlyFleets.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.listViewEnemyPiecesEnRoute.View = View.Tile;
            this.listViewEnemyPiecesEnRoute.TileSize = new Size(PlayingPieceGraphics.GetBlockSize().Width * 2, PlayingPieceGraphics.GetBlockSize().Height * 2);
            foreach (PlayingPiece playingPiece in piecesBeingTransported)
            {
                this.listViewEnemyPiecesEnRoute.Items.Add(playingPiece.GetDescription());
            }
            foreach(FleetPoint enemyFleet in enemyFleetPoints)
            {
                this.listViewEnemyFleets.Items.Add(enemyFleet.GetDescription());
            }
            foreach (FleetPoint friendlyFleet in friendlyFleetPoints)
            {
                this.listViewFriendlyFleets.Items.Add(friendlyFleet.GetDescription());
            }
        }

        private void listViewEnemyPiecesEnRoute_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            if(piecesBeingTransported[e.ItemIndex] is Block)
                PlayingPieceGraphics.PaintBlock(e.Graphics, (Block)piecesBeingTransported[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), 1, true);
            else if (piecesBeingTransported[e.ItemIndex] is GroundSupportUnit)
                PlayingPieceGraphics.PaintGSU(e.Graphics, (GroundSupportUnit)piecesBeingTransported[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void listViewFriendlyFleets_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, friendlyFleetPoints[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void listViewEnemyFleets_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, enemyFleetPoints[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void interceptButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void notIntercept_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void eyeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}